﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ZLDC.Common.Estate;
namespace ZLDC.Estate.Quantify.Report
{
    public partial class lp_Report_list : System.Web.UI.Page
    {
        public string deptid = "";
        public string statrday = "";
        public string endday = "";
        protected string station;
        public StringBuilder sb = new StringBuilder();
        public string depttype = "";//获取部门类型
      
        protected void Page_Load(object sender, EventArgs e)
        {
            deptid = _Request.GetString("deptid",_Utility.GetInfoBySession().m_sDeptID);
            statrday = _Request.GetString("statrday", "'"+DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")+"'");
            endday = _Request.GetString("endday", "'" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + "'");
            station = _Utility.GetInfoBySession().m_sStation;
            #region
            if (!IsPostBack)
            {
                if (deptid != "0")
                {
                    if (ViewState.Count == 0)
                    {
                        GetTableCount(deptid);
                    }
                    depttype= ZLDC.Topo.BLL.Estate.lp_Report_list.GetType(deptid);//获取部门类型
                    GetTB(deptid, statrday, endday);
                }
                else
                {
                    if (ViewState.Count == 0)
                    {
                        GetTableCount(_Utility.GetInfoBySession().m_sDeptID);
                    }
                    depttype = ZLDC.Topo.BLL.Estate.lp_Report_list.GetType(_Utility.GetInfoBySession().m_sDeptID);//获取部门类型
                    GetTB(_Utility.GetInfoBySession().m_sDeptID, statrday, endday);
                }
            }
            #endregion
        }

        /// <summary>
        /// 查询点击分行 是否有权限
        /// </summary>
        protected void GetTableCount(string deptid)
        {

            if (int.Parse(ZLDC.Topo.BLL.Estate.lp_Report_list.GetTableCount(deptid)) > 0)
            {
                ViewState["tbcount"] = "1";
            }
            
        }

       /// <summary>
       /// 页面查询显示条件，身份不同查询不同
       /// </summary>
       /// <param name="deptId"></param>
       /// <param name="startdate"></param>
       /// <param name="enddate"></param>
        protected void GetTB(string deptId, string startdate, string enddate)
        {
        //    #region 
        //    string sqlfile = "select SUM(total) as total,SUM(Actual_Goal) as Actual_Goal ";
        //    //string sql = "   where 1=1 and (total >0 or leave=1 or Other1='1')";
        //    string sql = "   where 1=1 ";
        //    string group = "group by   "; 
            string sqlwhere = " where 1=1 "; //饼图显示条件
        //    sb.Append("<table id=\"tb1\" width=\"70%\" cellpadding=\"0\" cellspacing=\"1\" border=\"0\">");
        //    sb.Append("<caption></caption>");
        //    sb.Append("<thead>");
        //    sb.Append("<tr>");
            if (depttype == "营业大部")
            {
        //        sqlfile += ",expr3,expr4 ";
        //        sql += " and expr5=" + deptId + ""; sqlwhere += "and vm.expr5=" + deptId + "";
        //        group += "expr3,expr4 order by total desc";
                sqlwhere += "and Other8='" + deptId + "'";
        //        sb.Append("<th align=\"center\" width=\"50\"  class=\"th\">");
        //        sb.Append("营业部");
        //        sb.Append("</th>");
            }
            else if (depttype == "营业部")
            {
        //        sqlfile += ",expr1,expr2 ";
        //        sql += " and expr3=" + deptId + "";
                sqlwhere += "and Other6='" + deptId + "'";
        //        group += "expr1,expr2 order by total desc";
        //        sb.Append("<th align=\"center\" width=\"50\"  class=\"th\">");
        //        sb.Append("区域");
        //        sb.Append("</th>");
            }
            else if (depttype == "区域")
            {
        //        sqlfile += ",id,name ";
        //        sql += " and expr1=" + deptId + "";
                sqlwhere += "and Other4='" + deptId + "'";
        //        group += "id,name order by total desc";
        //        sb.Append("<th align=\"center\" width=\"50\"  class=\"th\">");
        //        sb.Append("分行");
        //        sb.Append("</th>");
            }
            else if (depttype == "分行")
            {
                if (ViewState["tbcount"]!=null && ViewState["tbcount"].ToString()=="1")
                {
        //            sqlfile += ",userid,user_name,id ";
        //            sql += " and id=" + deptid + "";
                    sqlwhere += "and Other2=" + deptid + "";
        //            group += "userid,user_name,id order by total desc";
        //            sb.Append("<th align=\"center\" width=\"50\"  class=\"th\">");
        //            sb.Append("姓名");
        //            sb.Append("</th>");
                }
        //        else
        //        {
        //            sb.Append("<tr><td><a style='color:red'>无查询数据</a></td></tr>");
        //            return ;
        //        }
            }
        //    else {
        //        sb.Append("<tr><td><a style='color:red'>无查询数据</a></td></tr>");
        //        return; }
        //    sqlfile += " from v_lp_mainlist";
        //    sb.Append("<th align=\"center\" width=\"50\"  class=\"th\">");
        //    sb.Append("总分");
        //    sb.Append("</th>");
        //    sb.Append("</tr>");
        //    sb.Append("</thead>");
        //    sql = sqlfile + sql;
        //    sql += " and workday >=" + statrday + " and workday<=" + endday + " ";
            
        //    DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql+group);
        //    if (ds.Tables.Count > 0)
        //    {
        //        sb.Append("<tbody>");

        //        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        //        {
        //            sb.Append("<tr>");
        //            if (depttype == "营业大部")
        //            {
        //                sb.Append("<td class=\"td\">");
        //                sb.Append("<a href=\"lp_Report_list.aspx?deptid=" + ds.Tables[0].Rows[i]["expr3"].ToString() + "&statrday=" + statrday + "&endday=" + endday + "\">" + ds.Tables[0].Rows[i]["expr4"].ToString() + "</a>");
        //                sb.Append("</td>");
        //            }
        //            if (depttype == "营业部")
        //            {
        //                sb.Append("<td class=\"td\">");
        //                sb.Append("<a href=\"lp_Report_list.aspx?deptid=" + ds.Tables[0].Rows[i]["expr1"].ToString() + "&statrday=" + statrday + "&endday=" + endday + "\">" + ds.Tables[0].Rows[i]["expr2"].ToString() + "</a>");
        //                sb.Append("</td>");
        //            }
        //            if (depttype == "区域")
        //            {
        //                sb.Append("<td class=\"td\">");
        //                if (ViewState["tbcount"] != null && ViewState["tbcount"].ToString() == "1")
        //                {

        //                    sb.Append("<a href=\"lp_Report_list.aspx?deptid=" + ds.Tables[0].Rows[i]["id"].ToString() + "&statrday=" + statrday + "&endday=" + endday + "\">" + ds.Tables[0].Rows[i]["name"].ToString() + "</a>");

        //                }
        //                else
        //                {
        //                    sb.Append("<a href=\"#\">" + ds.Tables[0].Rows[i]["name"].ToString() + "</a>");
        //                }
        //                sb.Append("</td>");
        //            }
        //            if (depttype == "分行")
        //            {


        //                sb.Append("<td class=\"td\">");
        //                sb.Append("<a  onclick=\"FKalert(" + ds.Tables[0].Rows[i]["id"].ToString() + "," + ds.Tables[0].Rows[i]["userID"].ToString() + "," + startdate + "," + enddate + ",'" + ds.Tables[0].Rows[i]["user_name"].ToString() + "')\">" + ds.Tables[0].Rows[i]["user_name"].ToString() + "</a>");
        //                sb.Append("</td>");


        //            }
        //            sb.Append("<td class=\"td\">");
        //            sb.Append(ds.Tables[0].Rows[i]["Actual_Goal"].ToString());
        //            sb.Append("</td>");
        //            sb.Append("</tr>");
        //        }
        //    }
        //    else
        //    {
        //        sb.Append("<tr><td><a style='color:red'>无查询数据</a></td></tr>");
        //    }
        //    sb.Append("</tbody>");
        //    sb.Append("</table>");
            
        //    #endregion
            GetCharts(myNext, ZLDC.Topo.BLL.Estate.lp_Report_list.GetSql(sqlwhere,statrday,enddate));
        }

        /// <summary>
        /// 显示饼图
        /// </summary>
        /// <param name="mylist"></param>
        /// <param name="mynext"></param>
        public void GetCharts(Literal mynext,string sql)
        {

            StringBuilder sb1 = new StringBuilder();
            DataSet ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            string sstatryday = statrday.Replace("'","");
            string eendday = endday.Replace("'","");
            if (ds1.Tables.Count > 0)
            {
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    sb1.Append("{name:'" + ds1.Tables[0].Rows[i]["BasisName"].ToString() + ds1.Tables[0].Rows[i]["Actual_Number"].ToString() + "',");
                    sb1.Append(" y:" + ds1.Tables[0].Rows[i]["Actual_Goal"].ToString() + ", ");
                    sb1.Append("dataLabels:'" + ZLDC.Topo.BLL.Estate.lp_Basis.Get_ChartUrl(ds1.Tables[0].Rows[i]["BasisName"].ToString()) + "&deptid=" + deptid + "&addDate2=" + eendday + "&addDate1=" + sstatryday + "'},");

                }
            }
            hidchart.Value = sb1.ToString();

        }

        
    }
}
